<?php
namespace Common\Model;
use Think\Model;
class MessageModel extends Model{

	/**
	 * 添加消息
	 * @param integer $send_user_id 发送者用户ID，0系统发送
	 * @param [type]  $to_user_id   接收用户ID
	 * @param string  $title        消息标题
	 * @param string  $content      消息内容
	 * @param string  $msg_type     消息类型：系统消息system、订单消息order、账单消息account、服务消息todo
	 * @param string  $content_type 内容类型：text
	 * @param string  $relation_id  关联ID，如提现ID、订单号
	 */
	public function add_msg($send_user_id = 0, $to_user_id, $title = '', $content = '', $msg_type = 'system', $content_type = 'text', $relation_id = ''){
		if(!$to_user_id){
			return false;
		}
		$data['user_id']      = $to_user_id;
		$data['send_user_id'] = $send_user_id;
		$data['title']        = $title ? $title : '';
		$data['content']      = $content;
		$data['msg_type']     = $msg_type;
		$data['content_type'] = $content_type;
		$data['relation_id']  = $relation_id ? $relation_id : 0;
		$data['is_send_sms']  = $this->is_send_sms ? 1 : 0;
		$data['create_time']  = time();

		// 添加系统消息记录，
		$log = $data;
		$log['is_push']       = 1;// 是否进行推送
		$log['user_id']       = $to_user_id;
		$log['mobiles']       = $this->mobiles ? $this->mobiles : '';
		$log['status_desc']   = $this->status_desc ? $this->status_desc : '';
		$log['tpl_name']      = $this->tpl_name ? $this->tpl_name : '';
		$log['tpl_key']       = $this->tpl_key ? $this->tpl_key : '';
		$log['is_weixin_msg'] = $this->is_weixin_msg ? 1 : 0;
		D('MessageLog')->add($log);
		unset($log);

		$where['user_id']      = $to_user_id;
		$where['send_user_id'] = $send_user_id;
		$where['msg_type']     = $msg_type;
		if($this->where($where)->count()){
			//更新消息数量
			$data['unread_num'] = array('exp', 'unread_num+1');
			$this->where($where)->save($data);
			//自己消息列表也一起更新
			if($send_user_id != 0){
				$where['user_id']      = $send_user_id;
				$where['send_user_id'] = $to_user_id;
				$where['user_type']    = $send_user_type;
				unset($data['user_id'], $data['send_user_id'], $data['unread_num'], $data['is_sms_tiped']);
				$this->where($where)->save($data);
			}
		}else{
			$data['unread_num']    = 1;
			$data['is_allow_send'] = 1;
			$this->add($data);
			//自己消息列表也一起更新
			if($send_user_id != 0){
				$data['user_id']       = $send_user_id;
				$data['send_user_id']  = $to_user_id;
				$data['is_allow_send'] = 0;
				unset($data['unread_num'], $data['is_sms_tiped']);
				$this->add($data);
			}
		}
	}
	/**
	 * 系统消息推送
	 * @param  [type] $key [description]
	 * @return [type]      [description]
	 */
	public function system_msg_push($to_user_id = '', $key = ''){
		$system_push_tpl = C('system_push_tpl');
		$tpl = $system_push_tpl[$key];
		$content      = $tpl['content'];
		$user_type    = $tpl['to'];
		$is_send_sms  = $tpl['is_send_sms'];

		if(!$to_user_id || !$content){
			return;
		}
		// 消息标题
		$title  = '系统消息';
		//手机号
		$user_name = D('User')->get_user_name($to_user_id);
		$mobile    = D('User')->get_user_mobile($to_user_id);

		//要被替换字段
		$search = array(
			'{$user_name}',
			'{$mobile}',
			'{$user_id}',
		);
		//替换后字段
		$replace = array(
			$user_name,
			$mobile,
			$to_user_id,
		);
		//替换后内容(给对方)
		$content = str_replace($search, $replace, $content);

		//添加记录
		$this->is_send_sms   = $is_send_sms;// 是否发送短信
		$this->mobiles       = $mobile;// 短信接收手机号多个以逗号分隔
		$this->tpl_name      = 'system_push_tpl';
		$this->tpl_key       = $key;
		$this->is_weixin_msg = $tpl['weixin_tpl_id'] ? 1 : 0;
		$this->add_msg(0, $to_user_id, $title, $content, 'system', 'text');
	}
	/**
	 * 订单消息推送
	 * @param  [type] $order_sn 订单号
	 * @param  [type] $key      推送模板KEY
	 */
	public function order_msg_push($order_sn, $key){
		$order_push_tpl = C('order_push_tpl');
		$tpl = $order_push_tpl[$key];
		$content      = $tpl['content'];
		$status_desc  = $tpl['status_desc'];
		$is_send_sms  = $tpl['is_send_sms'];

		if(!$order_sn || !$content){
			return;
		}

		//模板替换
		$info  = $this->order_tpl_replace($order_sn, $content);
		//替换后内容(给对方)
		$content    = $info['content'];
		//手机号
		$to_mobile  = $info['mobile'];
		//接收用户ID
		$to_user_id = $info['user_id'];

		//添加记录
		$this->status_desc   = $status_desc;
		$this->is_send_sms   = $is_send_sms;// 是否发送短信
		$this->mobiles       = $to_mobile;// 短信接收手机号多个以逗号分隔
		$this->tpl_name      = 'order_push_tpl';
		$this->tpl_key       = $key;
		$this->is_weixin_msg = $tpl['weixin_tpl_id'] ? 1 : 0;
		$this->add_msg(0, $to_user_id, '订单通知', $content, 'order', 'text', $order_sn);

	}

	/**
	 * 交易消息推送
	 * @param  [type] $key       推送模板标识
	 * @param  [type] $object_id 对象ID
	 */
	public function bill_msg_push($relation_id, $key){
		$bill_push_tpl = C('bill_push_tpl');
		$tpl          = $bill_push_tpl[$key];
		$content      = $tpl['content'];
		$type         = $tpl['type'];
		$status_desc  = $tpl['status_desc'];
		$is_send_sms  = $tpl['is_send_sms'];

		if(!$content || !$relation_id){
			return false;
		}
		switch ($type) {
			case 'order':
				$info = $this->order_tpl_replace($relation_id, $content);
				break;
			case 'take_out':
				$info = $this->take_out_tpl_replace($relation_id, $content);
				break;
			case 'recharge_order':
				$info = $this->recharge_order_tpl_replace($relation_id, $content);
				break;
		}
		if(!$info){
			return false;
		}
		//状态
		$this->status_desc   = $status_desc;
		//替换后内容
		$content             = $info['content'];
		//接收用户ID
		$to_user_id          = $info['user_id'];
		//手机号
		$to_mobile           = $info['mobile'];
		$this->mobiles       = $to_mobile;// 短信接收手机号多个以逗号分隔
		$this->is_send_sms   = $is_send_sms;
		$this->tpl_name      = 'todo_push_tpl';
		$this->tpl_key       = $key;
		$this->is_weixin_msg = $tpl['weixin_tpl_id'] ? 1 : 0;
		//添加记录
		$this->add_msg(0, $to_user_id, '交易通知', $content, 'account', 'text', $relation_id);
	}
	//提现消息模板替换
	public function take_out_tpl_replace($take_out_id = '', $content = ''){
		$where['id'] = $take_out_id;
		$info = D('UserTakeOut')->where($where)->find();
		$user_id        = $info['user_id'];
		$user_name      = D('User')->get_user_name($info['user_id']);
		$user_mobile    = D('User')->get_user_mobile($info['user_id']);
		$money          = $info['money'];
		$reason_content = $info['remark'];

		//要被替换字段
		$search = array(
			'{$user_id}',
			'{$user_name}',
			'{$money}',
			'{$reason_content}',
		);
		//替换后字段
		$replace = array(
			$user_id,
			$user_name,
			$money,
			$reason_content,
		);
		$data['content'] = str_replace($search, $replace, $content);
		$data['user_id'] = $user_id;
		$data['mobile']  = $user_mobile;
		$data['search']  = $search;
		$data['replace'] = $replace;
		return $data;
	}
	/**
	 * 订单消息模板替换
	 * @param  string $order_sn     订单号
	 * @param  string $content      替换内容
	 * @param  string $self_content 替换内容
	 */
	public function order_tpl_replace($order_sn = '', $content = ''){
		$order = D('Order')->info($order_sn);

		$order_sn       = $order['order_sn'];
		$user_id        = $order['user_id'];
		$order_amount   = $order['order_amount'];
		$payed_amount   = $order['payed_amount'];
		$refund_amount  = $order['payed_amount'];
		$order_time     = date('Y年m月d日 H:i:s', $order['create_time']);
		

		//要被替换字段
		$search = array(
			'{$order_title}',
			'{$lawyer_realname}',
			'{$lawyer_mobile}',
			'{$user_name}',
			'{$order_amount}',
			'{$payed_amount}',
			'{$refund_amount}',
			'{$order_sn}',
			'{$order_type_desc}',
			'{$order_time}',
			'{$datetime}',
			'{$status_desc}',
		);
		//替换后字段
		$replace = array(
			$order['title'],
			$order['lawyer_name'],
			$order['lawyer_mobile'],
			$order['user_name'],
			$order_amount,
			$payed_amount,
			$refund_amount,
			$order_sn,
			$order['order_type_desc'],
			$order_time,
			date('Y-m-d H:i:s', $order['pay_time']),
			$order['status_desc'],
		);
		$data['content']    = str_replace($search, $replace, $content);
		$data['user_id']    = $user_id;
		$data['mobile']     = $order['user_mobile'];
		$data['search']     = $search;
		$data['replace']    = $replace;
		return $data;
	}
	/**
	 * 订单消息模板替换
	 * @param  string $order_sn     订单号
	 * @param  string $content      替换内容
	 * @param  string $self_content 替换内容
	 */
	public function recharge_order_tpl_replace($order_sn = '', $content = ''){
		$order = D('RechargeOrder')->where(array('order_sn' => $order_sn))->find();

		$user_id = $order['user_id'];
		//要被替换字段
		$search = array(
			'{$amount}',
			'{$usable_account}',
			'{$user_id}',
			'{$recharge_time}',
		);
		$usable_account = D('Account')->get_account_money($user_id);
		//替换后字段
		$replace = array(
			$order['amount'],
			$usable_account,
			$user_id,
			date('Y-m-d H:i:s', $order['pay_time']),
		);
		$data['content'] = str_replace($search, $replace, $content);
		$data['user_id'] = $user_id;
		$data['mobile']  = $mobile;
		$data['search']  = $search;
		$data['replace'] = $replace;
		return $data;
	}
	/**
	 * 订单客服确认消息
	 * @param  string $order_sn 订单号
	 */
	public function order_service_confirm($order_sn = ''){
		$where['order_sn'] = $order_sn;
		$info = D('Order')->where($where)->find();
		if(!$info['lawyer_user_id']){
			return false;
		}
		$lawyer_mobile = D('User')->get_user_mobile($info['lawyer_user_id']);
		$lawyer_name   = D('User')->get_user_name($info['lawyer_user_id']);
		$content       = $lawyer_name . '律师，您有一个新的订单，请及时处理';
		D('Sms')->send($lawyer_mobile, $content);
		return true;
	}
}